/**
 * @param {number[]} nums
 * @return {number}46
 */
console.log(maxSubArray([-2, 1, -3, 4, -1, 2, 1, -5, 4]));

function maxSubArray(nums) {
    for (var j = 0;
        (nums[j] <= 0) && (nums[j] + nums[j + 1] > 0); j++) {} //1
    return nums[j] + cycle(nums, j + 1);

};

function cycle(nums, i) { //找数组的第一个正数加后面数组的最大和
    var s = 0;
    for (i; s <= 0; i++) {
        if (nums[i] == null) { return 0; }
        s += nums[i];
    }
    return s + cycle(nums, i);
};